//
//  YMEmotiocnKeyboardView.swift
//  YMWeiBo
//
//  Created by mimi on 16/7/29.
//  Copyright © 2016年 mimi. All rights reserved.
//

import UIKit

class YMEmotiocnKeyboardView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREENW, height: 216))
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
   func setupUI() {
    backgroundColor = RandomColor()
    //add控件
    addSubview(emoticonToolbar)
    addSubview(emoticonPageView)
    addSubview(pageControl)
    
    
    //add约束
        emoticonToolbar.snp_makeConstraints { (make) in
            make.leading.trailing.bottom.equalTo(self)
            make.height.equalTo(37)
        
        emoticonPageView.snp_makeConstraints(closure: { (make) in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(emoticonToolbar.snp_top)
        })
            
            pageControl.snp_makeConstraints(closure: { (make) in
                make.centerX.equalTo(self)
                make.bottom.equalTo(emoticonPageView).inset(-5)
            })
            
            //底部视图按钮点击回调
            
            emoticonToolbar.closure = {[weak self] (type:YMEmoticonToolbarType) ->() in
                
                switch type{
                case .Default:
                    print("默认tool")
                case .Emoji:
                    print("表情tool")
                case .Lxh:
                    print("浪小花tool")
                }
                
                //获得indexpath
                let indexPath:NSIndexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
                
                self?.emoticonPageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false)
                
                self?.setupPageControlData(indexPath)
            }
            let indexPath = NSIndexPath(forItem: 0, inSection: 0)
            setupPageControlData(indexPath)
    }
    
    
    }
    //        MARK: - 懒加载
    private lazy var emoticonToolbar:YMEmoticonToolbar = YMEmoticonToolbar(frame: CGRectZero)
    private lazy var emoticonPageView:YMEmoticonPageView = {
        let view = YMEmoticonPageView()
        view.pagingEnabled = true
        view.bounces = false
        view.delegate = self
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        return view
    }()
    
    private lazy var pageControl:UIPageControl = {
       let view = UIPageControl()
        view.setValue(UIImage(named:"compose_keyboard_dot_normal"), forKey: "pageImage")
        view.setValue(UIImage(named:"compose_keyboard_dot_selected"), forKey: "currentPageImage")
        
        
        
        return view
    }()
}


//        MARK: - collectionView的代理方法
//监听滚动以及联动效果
extension YMEmotiocnKeyboardView:UICollectionViewDelegate{
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        //获取相对于collectionView 的中心点X
        let centerX = scrollView.contentOffset.x + SCREENW*0.5
    //拿到频幕显示的cell数组,最这个无序数组排序
        let cells = self.emoticonPageView.visibleCells().sort { (cell1, cell2) -> Bool in
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        
        //如果屏幕显示2个 我们才可以这样判断 (cell的宽度等于屏幕的宽度,cells元素不是一个的情况下)
        
        if cells.count == 2{
            
            let firstCell = cells.first!
            let lastCell = cells.last!
            
            //另一indexpath
            
            var indexPath:NSIndexPath
            //contains包含的意思<cell中是否包含这个点:主要看X值>
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)) {
                indexPath = self.emoticonPageView.indexPathForCell(firstCell)!
            }else{
                indexPath = self.emoticonPageView.indexPathForCell(lastCell)!
            }
            emoticonToolbar.setupCurrentButton(indexPath.section)
            
            //设置滚动的时候的pagecontrol
            setupPageControlData(indexPath)
        }
        
    }
    
    func setupPageControlData(indexpath:NSIndexPath) {
        
        pageControl.numberOfPages = YMEmoticonTools.sharedTools.allEmoticons[indexpath.section].count
        pageControl.currentPage = indexpath.item
    }
}





